package com.example.qxfw.qhtztj.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.example.qxfw.common.util.FastjsonUtil.test;
import com.example.qxfw.qhtztj.entity.AnomalyData;
import com.example.qxfw.qhtztj.entity.vo.AnomalyDataVo;
import com.example.qxfw.qhtztj.mapper.AnomalyDataMapper;
import com.example.qxfw.qhtztj.service.AnomalyDataService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Chery
 * @date 2025/01/16
 */
@Service
public class AnomalyDataServiceImpl implements AnomalyDataService {

    @Resource
    private AnomalyDataMapper anomalyDataMapper;

    @DS("master")
    @Transactional
    public List<AnomalyDataVo> getClimaticData(AnomalyData anomalyData) {
        List<AnomalyDataVo> qhtztj = new ArrayList<>();
        List<AnomalyDataVo> qhtztjResult = new ArrayList<>();
        try {
            if(anomalyData.getSumindicatorname().equals("最大风向/频率")  || anomalyData.getSumindicatorname().equals("极大风向/频率") )
                qhtztj = anomalyDataMapper.getWindClimaticData(anomalyData);
            else
            qhtztj = anomalyDataMapper.getClimaticData(anomalyData);
            if (anomalyData.getStationType() == 1) {
                qhtztjResult = qhtztj.stream()
                        .filter(data -> data.getStationid().startsWith("5"))
                        .collect(Collectors.toList());//国家站排名
            } else if (anomalyData.getStationType() == 2) {
                qhtztjResult = qhtztj.stream()
                        .filter(data -> !data.getStationid().startsWith("5"))
                        .collect(Collectors.toList());//区域站排名
            } else if (anomalyData.getStationType() == 3) {
                qhtztjResult = qhtztj;//区域站排名
            }
            return qhtztjResult;
        } catch (Exception e) {
            throw new RuntimeException("Error occurred while get anomalyData", e);
        }
    }
    @DS("master")
    @Transactional
    public List<AnomalyDataVo> getMsgClimaticCharacteristicsData(AnomalyData anomalyData) {
        LocalDate endDate = null;
        try {
            if(anomalyData.getSumindicatorname().equals("最大风向/频率" ) || anomalyData.getSumindicatorname().equals("极大风向/频率") )
                return anomalyDataMapper.getWindCharacteristicsData(anomalyData);
            return anomalyDataMapper.getMsgClimaticCharacteristicsData(anomalyData);//查询气候特征统计
        } catch (Exception e) {
            throw new RuntimeException("Error occurred while get anomalyData", e);
        }
    }


}
